package com.itheima.d11_bufferedwriter;

import java.io.*;

/**
 * 原始流 缓冲流性能分析
 */
public class TimeTest3 {
    //源文件
    private static final String SRC_FILE = "D:\\18数据类型.mp4";
    //目标文件
    private static final String DEST_FILE = "D:\\Java\\";
     public static void main(String[] args) {
        //性能太差，直接淘汰，禁用使用
//        copyFile1();
        //比较慢的，还可以接受
//        copyFile2();//0.208秒
        //非常慢，直接淘汰，禁用使用
//        copyFile3();//1.704秒
        //非常快，性能比较好，推荐使用
        copyFile4();//0.064秒
    }
    //使用高级字节流按照字节数组的形式复制文件
    private static void copyFile4() {
        long start = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "1.mp4");
                BufferedInputStream bis = new BufferedInputStream(is);
                BufferedOutputStream bos = new BufferedOutputStream(os);
        ){
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕，耗时：" + (end - start) / 1000.0 + "秒");
    }

    //使用高级字节流按照一个个字节的形式复制文件
    private static void copyFile3() {
        long start = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "3.mp4");
                BufferedInputStream bis = new BufferedInputStream(is);
                BufferedOutputStream bos = new BufferedOutputStream(os);
        ){
           int b;
           while ((b = bis.read()) != -1) {
               bos.write(b);
           }
        }catch (Exception e){
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕，耗时：" + (end - start) / 1000.0 + "秒");
    }

    //使用低级字节流按照字节数组的形式复制文件
    private static void copyFile2() {
        long start = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "2.mp4");
        ){
            byte[] bytes = new byte[1024];
            int len;
            while ((len = is.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕，耗时：" + (end - start) / 1000.0 + "秒");
    }

    //使用低级流按照一个一个字节的形式复制文件
    private static void copyFile1() {
        long start = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "1.mp4");
                ){
            int b;
            while ((b = is.read()) != -1) {
                os.write(b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕，耗时：" + (end - start) / 1000.0 + "秒");
    }
}
